<?php

namespace App\Controllers;

class Travel extends BaseController {

    public function getIndex() {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $records = $places->findAll();
        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
        $displayHeadings = [$headings[1], $headings[6], $headings[4]];
        $table->setHeading(array_map('ucfirst', $displayHeadings));
        foreach ($records as $record) {
            $Link = anchor("travel/showme/$record->id", $record->link);
            $table->addRow($record->name, "<img height='300' width='250' src='/image/$record->image'/>", $Link);
        }
        $template = ['table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright Rec'
        ];
        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

    public function getShowme($id) {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $record = $places->find($id);
        // get a template parser

        $table = new \CodeIgniter\View\Table();

        $table->addRow('name', $record['name']);
        $table->addRow('team',$record['team']);
        $table->addRow('birth',$record['birth']);
        $table->addRow('description', $record['description']);
        $table->addRow('link', $record['link']);
        $table->addRow('image', '<img src="/image/' . $record['image'] . '"/>');

        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright Rec'
        ];

        
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

}
